<template>
    <view style="padding: 0 30rpx;">
        <view class="flex flex-column shadowWrap" style="padding: 40rpx 26rpx 48rpx 26rpx;margin-bottom: 32rpx;" >
            <view class="flex">
                <image src="../../static/images/res.png" style="width: 110rpx; height: 110rpx; margin-right: 40rpx;" />
                <view class="flex1">
                    <view class="flex  between">
                        <view style="font-weight: 600; font-size: 32rpx;width: 400rpx;"class="oneWrap">{{info.name}}</view>
                        <view class="describe-text flex item-center" @click="isMorePopup=true">
                            更多
                            <u-icon color="#999" name="arrow-right" size="20rpx" />
                        </view>
                    </view>
                    <view class="flex" style="font-size: 24rpx; color: #999;margin-top: 32rpx;">
                        <view>上传：{{info.itemNum}}道试题</view>
                        <view style="margin: 0 20rpx;">|</view>
                        <view>创建时间：{{info.insertDate? info.insertDate.split(" ")[0]:"" }}</view>
                    </view>
                </view>
            </view>
            <view class="flex between" style="margin-top: 60rpx;">
                <view v-for="item in menuList" :key="item.title" class="flex item-center" @click="onHandleData(item.title)">
                    <image :src="item.icon" style="width: 32rpx; height: 32rpx;margin-right: 20rpx;"/>
                    <text>{{ item.title }}</text>
                </view>
            </view>
        </view>
        <BotMenu :isPopup="isMorePopup" @close="isMorePopup = false" title="题库名称" :showCancel="false">
            <view class="botMenuOkBtn center" v-for="item in moreMenuList" :key="item.text"
                :style="{ color: item.code == 'deleteBank' ? 'red' : '#000' }"
                @click="onHandleMore(item)"
                >
                {{ item.text }}
            </view>
        </BotMenu>
        <!-- 编辑题库 -->
        <BotMenu :isPopup="isEditPopup" @close="quName = ''; isEditPopup = false" title="编辑题库" :showCancel="true">
            <view class="addNewQb">
                <u--input placeholder="请输入题库名称" style="height: 120rpx;" :border="'none'" :clearable="true"
                    v-model="quName"></u--input>
            </view>
            <view class="botMenuOkBtn center" @click="addQuestionBank">
                确定
            </view>
        </BotMenu>
        <!-- <NoOpenPopup :isPopup="isPopup" @close="isPopup=false"/> -->
    </view>
</template>
<script>
import addIcon from '../../static/images/question/addIcon.svg'
import enterIcon from '../../static/images/question/enterIcon.svg'
import exportIcon from '../../static/images/question/exportIcon.svg'
import {toTop,delSubject,saveSubject} from "../../myQuestionBank/services/myQuestionBankService"
import { notificationFun, onShowTipInfo } from '../../utils/PrjUtils';
export default {
    name: "QuestionItem",
    data() {
        return {
            menuList:[
                {
                    icon:exportIcon,
                    title:"导出题库",
                },
                {
                    icon:addIcon,
                    title:"添加章节",
                },
                {
                    icon:enterIcon,
                    title:"录入试题",
                }
            ],
            // 暂无开放的弹窗
            isPopup:false,
            
            moreMenuList: [
                { text: '置顶', code: 'pin' },
                { text: '编辑题库', code: 'editBank' },
                { text: '编辑章节', code: 'editChapter' },
                { text: '编辑试题', code: 'editQuestion' },
                { text: '录入试题', code: 'inputQuestion' },
                { text: '删除题库', code: 'deleteBank' }
            ],
            // 更多
            isMorePopup: false,
            // 编辑题库
            isEditPopup: false,
            // 编辑题库信息
            quName: ""
        }
    },
    props: {
        info:{
            type: Object,
            default: {
                insertDate: "",
                itemNum: 0,
                name: "",
                subjectUuid:"",
                id:""
            }
        }
    },
    methods: {
        onHandleData(title){
            if(title == "添加章节"&&onShowTipInfo(this.info.doing)){
                return;
            }
            this.$emit("callback",title)
        },
        // 操作更多
        onHandleMore(record){
            if(!["pin","inputQuestion"].includes(record.code)){
                // 删除单独提示
                if(this.info.doing==1&&record.code=="deleteBank"){
                    notificationFun("正在进行人工导题，暂时不能删除题库");
                    this.isMorePopup = false
                    return; 
                }
                else if(this.info.doing==2&&record.code=="deleteBank"){
                    notificationFun("正在进行Ai智能生成试题，暂时不能删除题库");
                    this.isMorePopup = false
                    return; 
                }
                if(onShowTipInfo(this.info.doing)){
                    this.isMorePopup = false
                    return; 
                }
            }
            switch (record.code) {
                case "pin":
                    this.onTop()
                    break;
                case "editBank":
                    this.isEditPopup = true
                    break;
                case "editChapter":
                    uni.navigateTo({
                        url: `/myQuestionBank/addQuestionBank?info=${encodeURIComponent(JSON.stringify({...this.info,title:"编辑章节"}))}`
                    })
                    break;
                case "editQuestion":
                    uni.navigateTo({
                        url: `../../myQuestionBank/editQuestionPage?info=${encodeURIComponent(JSON.stringify({...this.info}))}`
                    })
                    break;
                case "inputQuestion":
                    uni.navigateTo({
                        url: `/myQuestionBank/testQuestionsInput?info=${encodeURIComponent(JSON.stringify({...this.info}))}`
                    })
                    break;
                case "deleteBank":
                    this.onDelte()
                    break;
                default:
                    break;
            }
            this.isMorePopup = false
        },  
        // 新增题库
        async addQuestionBank() {
            let res = await saveSubject({ name: this.quName,id:this.info.id })
            notificationFun("操作成功")
            this.isEditPopup=false
            this.$emit("onRefresh")
            // this.onRefresh()
        },
        // 置顶
        onTop(){
            toTop({subjectUuid:this.info.subjectUuid}).then(res=>{
                notificationFun("置顶成功")
                this.isMorePopup=false;
                this.$emit("onRefresh")
            })
        },
        // 删除
        onDelte(){
            delSubject({subjectUuid:this.info.subjectUuid}).then(res=>{
                notificationFun("删除成功")
                this.isMorePopup=false;
                this.$emit("onRefresh")
            })
        }

    }
}

</script>

<style scoped>
    .shadowWrap{
        box-shadow: 0px 4px 12px 0px #00000014;
        border-radius: 24rpx;
    }
    .addNewQb {
        box-sizing: border-box;
        padding: 30rpx;
        background: #F9F9F9;
        margin: 0 30rpx;
        border-radius: 24rpx;
        margin-bottom: 34rpx;
    }

</style>
